package rm.metrology.shared;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Embedded;
import javax.persistence.Id;
import javax.persistence.PrePersist;

import com.googlecode.objectify.annotation.Unindexed;


public abstract class AbstractEntity implements Serializable {

	
	private static final long serialVersionUID = 1L;

	@Id 
	private Long fId;
	
	@Unindexed 
	private Long fCreatedAt = System.currentTimeMillis();
	
	private Long fModifiedAt;
	
	@Embedded
	protected List<LogEntity> logs = new ArrayList<LogEntity>();

	public final Long getId() {
		return fId;
	}
	
	public final Long getCreatedTime() {
		return fCreatedAt;
	}
	
	public final Long getLastModifiedTime() {
		return fModifiedAt;
	}
	
	public final List<LogEntity> getLogs() {
		return Collections.unmodifiableList(logs);
	}
	
	protected final boolean log(String staff, String comment) {
		return logs.add(new LogEntity(staff, comment));
	}
	
	@Override
	public int hashCode() {
		return 11 * getClass().getName().hashCode() + 23 * fId.hashCode();
	}
	
	@SuppressWarnings("unused")
	@PrePersist
	private void updatedModifiedTime() {
		fModifiedAt = System.currentTimeMillis();
	}
	
}
